package com.lianwei.hospital.controller;

import com.lianwei.hospital.Service.Impl.LiveInfoImpl;
import com.lianwei.hospital.Service.LiveInfoService;
import com.lianwei.hospital.module.entity.LiveInfo;
import com.lianwei.hospital.module.vo.LiveVO;
import com.lianwei.hospital.utils.PageUtil;
import com.lianwei.hospital.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/live")
public class LiveInfoController {
    @Autowired
    private LiveInfoService liveInfoService;

    @GetMapping("/selectById")
    public Result<LiveInfo> Select(Long i) throws Exception {
        System.out.println(i+"\n\n");
        LiveInfo liveInfo = liveInfoService.selectByPrimaryKey(i);
        return Result.buildResult(Result.Status.OK,liveInfo);
    }

    @PostMapping
    public Result<List<LiveInfo>> Insert(@RequestBody LiveInfo liveInfo) throws Exception {
        System.out.println(liveInfo);
        if(liveInfoService.insert(liveInfo)){
            List<LiveInfo> liveInfoList = liveInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,liveInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @PostMapping("deleteById")
    public Result<List<LiveInfo>> Delete(@RequestBody LiveInfo liveInfo) throws Exception {
        System.out.println(liveInfo);
        if(liveInfoService.deleteByPrimaryKey(liveInfo.getId())){
            List<LiveInfo> liveInfoList = liveInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,liveInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @PutMapping
    public Result<List<LiveInfo>> Update(@RequestBody LiveInfo liveInfo) throws Exception {
        System.out.println(liveInfo);
        if(liveInfoService.updateByPrimaryKey(liveInfo)){
            List<LiveInfo> liveInfoList = liveInfoService.selectAll();
            return Result.buildResult(Result.Status.OK,liveInfoList);
        }
        return Result.buildResult(Result.Status.BAD_REQUEST);
    }

    @GetMapping("/selectAll")
    public Result<PageUtil<LiveInfo>> list( LiveVO query) throws Exception {
        PageUtil<LiveInfo> pageUtil=liveInfoService.findPage(query);
        return Result.buildResult(Result.Status.OK,pageUtil);
    }
}
